// APP对象
const app = getApp()
// 工具函数
const util = require('../../utils/util')
// 获取短信验证码
const sendLoginCode = require('../../config').sendLoginCode
// 图片上传
const smallProgram = require('../../config').smallProgram

const TimeOut = 120

Page({
  data: {
    phone: {
      value: '',
      focus: true,
      placeholder: '手机号码'
    },
    yzm: {
      value: '',
      focus: true,
      disabled: false,
      placeholder: '请输入验证码'
    },
    yzmTime: TimeOut,
    logining: false
  },
  // 页面加载
  onLoad: function (options) {
   
  },
  // 页面首次渲染完
  onReady: function () {
  },

  // 点击获取验证码按钮获取验证码
  tapYzmBtn: function (e) {
    var phone = this.data.phone.value
    if (!phone) {
      util.alertMsg('手机号码为空!')
      return
    }

    if(!util.checkPhone(phone)){
      util.alertMsg('手机号码格式有误，请重填！!')
      return 
    }

    // 防止重复点击
    if (this.data.yzm.disabled){
      return
    }
    var yzm = this.data.yzm
    yzm.disabled = true
    this.setData({
      yzm: yzm
    })
    
    var self = this
    var yzmBtn = setInterval(function () {
      var yzmTime = self.data.yzmTime
      if (!yzmTime){
        clearInterval(yzmBtn)
        yzm = self.data.yzm
        yzm.disabled = false
        self.setData({
          yzmTime: TimeOut,
          yzm: yzm
        })
        return
      }
      yzmTime--
      self.setData({
        yzmTime: yzmTime
      })
    }, 1000)
    
    var options = {
      url: sendLoginCode,
      data: { 'mobile': this.data.phone.value,"type":1},
      success: function(){
        util.alertMsg('验证码已发送，请注意查收！!')
      },
      fail: function (data) {
        if (data.code === 1) {
          util.alertMsg(data.msg)
          self.setData({
            yzmTime: 0
          })
        } else {
          util.alertMsg('服务器繁忙！')
        }
      }
    }

    util.XHR(options)
  },
  // phone值绑定
  phoneInput: function (e) {
    var phone = this.data.phone
    var value = e.detail.value
    phone.value = value
    this.setData({
      phone: phone
    })
  },
  // yzm值绑定
  yzmInput: function (e) {
    var yzm = this.data.yzm
    var value = e.detail.value
    yzm.value = value
    this.setData({
      yzm: yzm
    })
  },
   // 登录验证
  // 获取用户头像
  onGotUserInfo: function (e) {
    var info = e.detail.userInfo; 
    var self = this
    var phone = this.data.phone.value
    var yzm = this.data.yzm.value;
    var headimg='';
    if (e.detail.userInfo) {//允许授权
      wx.setStorageSync('SESSION_NAME', info.nickName)
      headimg = info.avatarUrl; 
    } else {//拒绝授权,使用本地头像
      headimg = '../../image/default_user.png'
    }
      wx.getImageInfo({
        src: headimg,
        success: function (data) {  
          var header = {
            'Content-Type': 'multipart/form-data',
            'Cookie': 'third_session=' + wx.getStorageSync('SESSION_KEY')
          }
          
          // 上传图片 
          wx.uploadFile({
            url: smallProgram,
            filePath: data.path,//图片的本地路径
            name: 'image',
            header: header,
            formData: {
              path: 'comment'
            },
            success: function (res) { 
              if (res.statusCode === 200) {
                var data = JSON.parse(res.data)
                if (data.code === 1) { 
                  wx.setStorageSync('SESSION_ICON', data.t.images[0]) 

                  if (!phone) {
                    util.alertMsg({
                      content: '手机号码为空！',
                      success: function (res) {
                        phone = self.data.phone
                        phone.focus = true
                        self.setData({
                          phone: phone
                        })
                      }
                    })
                    return
                  }

                  if (!util.checkPhone(phone)) {
                    util.alertMsg({
                      content: '手机号码格式有误，请重填！',
                      success: function (res) {
                        phone = self.data.phone
                        phone.focus = true
                        self.setData({
                          phone: phone
                        })
                      }
                    })
                    return
                  }

                  if (!yzm) {
                    util.alertMsg({
                      content: '验证码为空！',
                      success: function (res) {
                        yzm = self.data.yzm
                        yzm.focus = true
                        self.setData({
                          yzm: yzm
                        })
                      }
                    })
                    return
                  }


                  // 防止重复点击
                  var logining = self.data.logining
                  if (logining) {
                    return
                  }
                  self.setData({
                    logining: true
                  })

                  util.showLoading('登录中···')

                  // 设置手机和验证码缓存
                  wx.setStorageSync('SESSION_MOBILE', phone)
                  wx.setStorageSync('SESSION_CODE', yzm)
                  // 初始化手机登录
                  app.isLogin({
                    success: function (isNewMem) {
                      util.alertMsg({
                        content: '登录成功！',
                        icon: 'success',
                        success: function () {
                          var url
                          if (isNewMem) {
                            // url = '/pages/loginStep1/loginStep1'
                          } else {
                            url = '/pages/index/index'
                          }
                          wx.reLaunch({
                            url: url,
                            success: function () {
                              console.log(2)
                            },
                            fail: function (res) {
                              console.log(3)
                            }
                          })
                        }
                      })
                    },
                    fail: function (errMsg) {
                      wx.removeStorageSync('SESSION_MOBILE', phone)
                      wx.removeStorageSync('SESSION_CODE', yzm)
                      util.alertMsg(errMsg.msg)

                      // 重置验证码定时器
                      self.setData({
                        yzmTime: 0
                      })
                    },
                    complete: function () {
                      self.setData({
                        logining: false
                      })
                      util.hideLoading()
                    }
                  })  
                }
              }
            },
            fail:function(res){
              console.log(res)
              
            }
          })
        }
      })
  },
 
  tapLoginBtn: function () {

  }
})